#!/bin/bash
#红色背景
red_bg='\e[41;33;1m'
#红色字体
red_zt='\e[1;31m'
#绿色背景
green_bg='\033[0;32;1m'
#绿色字体
green_zt='\e[1;32m'
#关闭效果
end='\033[0m'
#字体闪烁
ss='\033[05m'
#消隐
sy='\33[08m'
##常用语句定义区
print_green_bg_red_zt(){
	echo -e "${green_bg}${ss}${green_bg}正在进入...${end}${end}${end}\n${red_zt}${1}...${end}"
}
print_red_z_green_z_info(){
	echo -e "${red_zt}${1}:${end}${green_zt}${2}${end}"
}
cheks(){
	echo -e "${red_zt}正在检测${1}...${end}"
}
print_green_zt(){
	echo -e "${green_zt}${1}${end}"
}
print_red_z_green_z(){
	echo -e "${red_zt}${1}${end}${green_zt}${2}${end}${green_zt}${3}${end}"
}
print_red_z(){
	echo -e "${red_zt}${1}${end}"
}
print_red_z_b(){
	echo -e "${red_bg}${1}${end}"
}
print_red_z_z_bg(){
	echo -e "${red_zt}${1}${end}${red_bg}${2}${end}"
}
input(){
	echo -e "${red_zt}${1}${end}"
	read $2
}
ststus(){
    if [[ "$?" -eq "0" ]];then
        print_red_z_green_z "$1" "成功"
    else
        print_red_z_b "$1" "失败"
    fi
}
service_start(){
    systemctl restart $1
    ststus "启动服务[$1]"
}
service_install(){
    if [[ -e '/usr/bin/apt' ]];then
        apt install -y $1
        ststus "安装[$1]"
    elif [[ -e "/usr/bin/yum" ]];then
        if [[ -e './yum-config.sh' ]];then
            source ./yum_config.sh
            yum_config
        fi
        yum install -y $1
        ststus
    else
        print_red_z "无法判断当前系统包管理器"
        exit 1
    fi
}

service_install "rsyslog net-tools"
input "请输入日志保存路径,默认[/data/log/]" "log_dir"
if [[ -z $log_dir ]];then
    log_dir='/data/log'
fi
ips=`hostname -I | awk '{print $1}'`
input "请输入本机IP地址[对客户端访问],默认使用[$ips]" "IP"
if [[ -z $IP ]];then
    IP=$ips
    if [[ -z $IP ]];then
        print_red_z "无法检测到有效IP地址"
        exit 1
    fi
fi
mkdir -p $log_dir
config_file='/etc/rsyslog.conf'
config_file_bak='/etc/rsyslog.conf.bak'
if [[ ! -e $config_file_bak ]];then
    cp $config_file $config_file_bak
fi
cat <<EOF > $config_file
module(load="imuxsock") # provides support for local system logging
module(load="imklog")   # provides kernel logging support

module(load="imudp")
input(type="imudp" port="514")

module(load="imtcp")
input(type="imtcp" port="514")
$template IpTemplate,"this_save_dir/%FROMHOST-IP%.log"
*.*  ?IpTemplate
& ~



$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

$FileOwner root
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022

$WorkDirectory /var/spool/rsyslog

$IncludeConfig /etc/rsyslog.d/*.conf



auth,authpriv.*			/var/log/auth.log
*.*;auth,authpriv.none		-/var/log/syslog
daemon.*			-/var/log/daemon.log
kern.*				-/var/log/kern.log
lpr.*				-/var/log/lpr.log
mail.*				-/var/log/mail.log
user.*				-/var/log/user.log

mail.info			-/var/log/mail.info
mail.warn			-/var/log/mail.warn
mail.err			/var/log/mail.err

*.=debug;\
	auth,authpriv.none;\
	news.none;mail.none	-/var/log/debug
*.=info;*.=notice;*.=warn;\
	auth,authpriv.none;\
	cron,daemon.none;\
	mail,news.none		-/var/log/messages

*.emerg				:omusrmsg:*
*. *  @@IP_reomte_syslog_server:514
EOF
sed -i -e "s#this_save_dir#$log_dir#" -e "s#IP_reomte_syslog_server#$IP#g" $config_file
service_start "rsyslog"
up=`netstat -tulpn | grep rsyslog`
if [[ -z $up ]];then
    print_red_z_b "[rsyslog]服务启动检测\t启动失败"
else
    netstat -tulpn | grep rsyslog 
    print_red_z_b "[rsyslog]服务启动检测\t启动成功"
    print_green_zt "服务配置完成\n日志保存路径为:[ $log_dir ]\n本机配置IP地址:[ $IP ]"
fi